package com.sniff.atp.service.purchase;

import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.DKLoginDTO;
import com.sniff.atp.enums.api.PurchaseAPI;
import com.sniff.atp.service.login.DKLoginService;
import com.sniff.atp.utils.Headers;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.purchase
 * @Author: zxy
 * @CreateTime: 2022-11-01 14:31
 * @Description: 档口小程序拿货流程
 * @Version: 1.0
 */
@Service
public class DKPurchaseService {
    @Autowired
    private DKLoginService dkLoginService;

    private DKLoginDTO dkLoginDTO;

    /**
     * 小程序：根据仓库选择登录的用户
     */
    public DKLoginDTO dklogin(String stallName){
        this.dkLoginDTO = dkLoginService.login(stallName);
        return this.dkLoginDTO;
    }

    /**
     * 小程序：查询拿货列表
     */
    public String pageTakeOrder(String marketCode,String purcharseOrder, String status){
        JSONObject param = JsonUtil.readJson("purchase/pageTakeOrder");
        param.replace("marketCode", marketCode);
        param.replace("keyword", purcharseOrder);
        param.replace("status", status);

        return new HttpClientUtil(Headers.setDKHeader(dkLoginDTO)).doPostJson(PurchaseAPI.PAGE_TAKE_ORDER.getApi(), param);
    }

    /**
     * 小程序：档口接单
     */
    public String takeOrders(String idList){
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(idList);
        JSONObject param = new JSONObject();
        param.put("idList", arrayList);

        return new HttpClientUtil(Headers.setDKHeader(dkLoginDTO)).doPostJson(PurchaseAPI.TAKE_ORDERS.getApi(), param);
    }

    /**
     * 小程序：添加拿货记录提交
     */
    public String saveTakeRecord(String takeOrderId, int num){
        JSONObject param = new JSONObject();
        param.put("takeOrderId", takeOrderId);
        param.put("num", num);

        return new HttpClientUtil(Headers.setDKHeader(dkLoginDTO)).doPostJson(PurchaseAPI.SAVE_TAKE_RECORD.getApi(), param);
    }
}
